﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Waegis.Client.Components
{
    /// <summary>
    /// Serilizes the objects to XML
    /// </summary>
    internal static class WaegisSerializer
    {
        #region Public Methods

        /// <summary>
        /// Serializes a Post object to XML
        /// </summary>
        /// <param name="siteInstance">Site instance</param>
        /// <param name="post">Post</param>
        /// <returns>The serialized XML</returns>
        public static string SerializePost(string siteInstance, Post post)
        {
            return GetSerializedPost(siteInstance, post);
        }

        /// <summary>
        /// Serializes a site instance to XML
        /// </summary>
        /// <param name="siteInstance">Site instance</param>
        /// <returns>The serialized XML</returns>
        public static string SerializeSiteInstance(string siteInstance)
        {
            return GetSerializedSiteInstance(siteInstance);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Serializes a Post object to XML
        /// </summary>
        /// <param name="siteInstance">Site instance</param>
        /// <param name="post">Post</param>
        /// <returns>The serialized XML</returns>
        private static string GetSerializedPost(string siteInstance, Post post)
        {
            StringWriter stringWriter = new StringWriter();

            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

            xmlTextWriter.WriteStartElement("post");

            // Site Instance
            xmlTextWriter.WriteElementString("instance", siteInstance);

            // Permalink
            xmlTextWriter.WriteElementString("permalink", post.Permalink.ToString());

            // Post Type
            xmlTextWriter.WriteElementString("postType", post.PostType.ToString().ToLower());

            // Body
            xmlTextWriter.WriteElementString("body", post.Body);

            // Title
            if (!string.IsNullOrEmpty(post.Title))
                xmlTextWriter.WriteElementString("title", post.Title);

            // Author Name
            if (!string.IsNullOrEmpty(post.AuthorName))
                xmlTextWriter.WriteElementString("authorName", post.AuthorName);

            // Author Url
            if (post.AuthorUrl != null)
                xmlTextWriter.WriteElementString("authorUrl", post.AuthorUrl.ToString());

            // Author Email
            if (!string.IsNullOrEmpty(post.AuthorEmail))
                xmlTextWriter.WriteElementString("authorEmail", post.AuthorEmail);

            // Referrer
            if (post.Referrer != null)
                xmlTextWriter.WriteElementString("referrer", post.Referrer.ToString());

            // Client IP
            if (post.UserIP != null)
                xmlTextWriter.WriteElementString("clientIP", post.UserIP.ToString());

            // Client Agent
            if (!string.IsNullOrEmpty(post.UserAgent))
                xmlTextWriter.WriteElementString("clientAgent", post.UserAgent);


            xmlTextWriter.WriteEndElement();

            xmlTextWriter.Flush();
            xmlTextWriter.Close();
            stringWriter.Flush();

            return stringWriter.ToString();
        }

        /// <summary>
        /// Serializes a site instance to XML
        /// </summary>
        /// <param name="siteInstance">Site instance</param>
        /// <returns>The serialized XML</returns>
        private static string GetSerializedSiteInstance(string siteInstance)
        {
            StringWriter stringWriter = new StringWriter();

            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

            xmlTextWriter.WriteStartElement("instance");

            xmlTextWriter.WriteElementString("url", siteInstance);

            xmlTextWriter.WriteEndElement();


            xmlTextWriter.Flush();
            xmlTextWriter.Close();
            stringWriter.Flush();

            return stringWriter.ToString();
        }

        #endregion
    }
}
